package com.leyou.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.dto.SpecParamDTO;
import com.leyou.mapper.SpuDetailMapper;
import com.leyou.pojo.Spu;
import com.leyou.pojo.SpuDetail;
import com.leyou.service.SpecParamService;
import com.leyou.service.SpuDetailService;
import com.leyou.service.SpuService;
import com.leyou.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.reflect.generics.scope.Scope;

import java.util.List;
import java.util.Map;

@Service
public class SpuDetailServiceImpl extends ServiceImpl<SpuDetailMapper, SpuDetail> implements SpuDetailService {
    @Autowired
    private SpuDetailService spuDetailService;
    @Autowired
    private SpecParamService specParamService;
    @Autowired
    private SpuService spuService;

    /**
     * 根据商品id和是否参与搜索查询spu对应的所有参数
     * @param id
     * @param searching
     * @return
     */
    @Override
    public List<SpecParamDTO> queryBySpuId(Long id, Boolean searching) {
        SpuDetail spuDetail = spuDetailService.query().eq("spu_id", id).one();
        Map<Long, Object> specValues = JsonUtils.toMap(spuDetail.getSpecification(), Long.class, Object.class);
        Spu spu = spuService.getById(id);
       List<SpecParamDTO> list= specParamService.queryByCategoryAndSearch(spu.getCid3(),searching);
        for (SpecParamDTO specParamDTO : list) {
            specParamDTO.setValue(specValues.get(specParamDTO.getId()));
        }
        return list;
    }
}
